package com.hnkywl.sts.action.cwgl.yjyys;

import com.hnkywl.sts.entity.cwgl.yjyys.Yjyys;
import com.hnkywl.sts.service.cwgl.yjyys.YjyysService;
import com.hnkywl.sts.service.sys.YgService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.hibernate.tool.hbm2x.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 灵韵
 * Date: 13-9-28
 * Time: 上午10:00
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/cwgl/yjyys")
public class YjyysAction extends SimpleActionSupport {
    private YjyysService yjyysService;
    private Integer nf;
    private Integer yf;
    private Long gsId;
    private Long bmId;
    private Long id;
    private int page;
    private int rows;
    private Yjyys yjyys;
    private List<Map> items;

    @Action("yjyys")
    public String yjyys(){

//        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        nf = Calendar.getInstance().get(Calendar.YEAR);
        yf = Calendar.getInstance().get(Calendar.MONTH)+1;
        gsId = YgService.getCurrentYgZgGsId();
        return SUCCESS;
    }

    @Action("yjyysList")
    public void yjyysList(){
        renderJson(yjyysService.yjyysGrid(gsId, bmId, nf, yf, page, rows));
    }

    @Action("savemx")
    public void savemx(){
        try{
            yjyysService.save(yjyys);
            renderText("{\"statusCode\":200}");
        } catch (Exception e){
            e.printStackTrace();
            renderText("{\"statusCode\":300,\"message\":\"" + e.getMessage() + "\"}");
        }
    }
    @Action("yjyys_content")
    public String yjyysContent(){
        if(id != null){
            yjyys = yjyysService.findDetail(id);
            gsId = yjyys.getGs().getId();
            if(yjyys.getBm() != null){
                bmId = yjyys.getBm().getId();
            }
            nf = yjyys.getNf();
            yf = yjyys.getYf();
        } else {
            nf = Calendar.getInstance().get(Calendar.YEAR);
            yf = Calendar.getInstance().get(Calendar.MONTH)+1;
            gsId = YgService.getCurrentYgZgGsId();
            items = yjyysService.findMx();
        }
        return SUCCESS;
    }

    @Action("yjyys_hz")
    public String yjyysHz(){
        nf = Integer.valueOf(new SimpleDateFormat("yyyy").format(new Date()));
        yf = Integer.valueOf(new SimpleDateFormat("MM").format(new Date()));
        return SUCCESS;
    }
    @Action("hzList")
    public void findHzList(){
        renderJson(yjyysService.findHz(nf,yf));
    }

    @Action("delete")
    public void delete(){
        try{
            yjyysService.delete(id);
            renderJson("{\"statusCode\":200}");
        } catch (Exception e){
            e.printStackTrace();
            renderJson("{\"statusCode\":200,\"message\":\""+e.getMessage()+"\"}");
        }
    }

    @Action("submit")
    public void submit(){
        try{
            yjyysService.submit(id);
            renderJson("{\"statusCode\":200}");
        } catch (Exception e){
            e.printStackTrace();
            renderJson("{\"statusCode\":200,\"message\":\""+e.getMessage()+"\"}");
        }
    }

    @Action("sh")
    public void sh(){
        try{
            yjyysService.sh(id);
            renderJson("{\"statusCode\":200}");
        } catch (Exception e){
            e.printStackTrace();
            renderJson("{\"statusCode\":200,\"message\":\""+e.getMessage()+"\"}");
        }
    }


    public Integer getNf() {
        return nf;
    }

    public void setNf(Integer nf) {
        this.nf = nf;
    }

    public Integer getYf() {
        return yf;
    }

    public void setYf(Integer yf) {
        this.yf = yf;
    }

    public Long getGsId() {
        return gsId;
    }

    public void setGsId(Long gsId) {
        this.gsId = gsId;
    }

    public Long getBmId() {
        return bmId;
    }

    public void setBmId(Long bmId) {
        this.bmId = bmId;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public Yjyys getYjyys() {
        return yjyys;
    }

    public void setYjyys(Yjyys yjyys) {
        this.yjyys = yjyys;
    }

    public List<Map> getItems() {
        return items;
    }

    public void setItems(List<Map> items) {
        this.items = items;
    }

    @Autowired
    public void setYjyysService(YjyysService yjyysService) {
        this.yjyysService = yjyysService;
    }
}
